//
//  TearBookViewController.m
//  VideoPlayer
//
//  Created by SKar on 15/6/4.
//  Copyright (c) 2015年 SKar. All rights reserved.
//

#import "TearBookViewController.h"
#import <MediaPlayer/MediaPlayer.h>

@interface TearBookViewController ()

@property (weak, nonatomic) IBOutlet UIButton *tearBookButton;
@property (nonatomic, strong) UIView *alertView;

@property (nonatomic, strong) NSMutableArray *videoArray;
@property (nonatomic, copy) NSURL *currentVideoPath;
@property (nonatomic, copy) NSArray *bookArray;
@property (nonatomic, copy) NSString *currentImageName;

@property (nonatomic, strong) MPMoviePlayerController *movie;

- (IBAction)playTearBookVideo:(id)sender;
- (IBAction)switchToLastBook:(id)sender;
- (IBAction)switchToNextBook:(id)sender;
- (IBAction)dismissViewController:(id)sender;

@end

@implementation TearBookViewController

- (instancetype)initWithNib {
    self = [super initWithNibName:NSStringFromClass([TearBookViewController class]) bundle:nil];
    if (self) {
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.videoArray = [NSMutableArray array];
    for (int i = 1; i <= 3; i++) {
        NSURL *filePath = [[NSBundle mainBundle] URLForResource:[NSString stringWithFormat:@"book%d",i] withExtension:@"mp4"];
        [self.videoArray addObject:filePath];
    }
    //初始化bookArray
    self.bookArray = @[@"SI.png"];
    NSString *defaultImageName = [self.bookArray firstObject];
    [self setBookButtonImageWithImageName:defaultImageName];
}

- (void)setBookButtonImageWithImageName:(NSString *)name {
    __weak TearBookViewController *weakSelf = self;
    dispatch_async(dispatch_get_main_queue(), ^{
        __strong TearBookViewController *strongSelf = weakSelf;
        strongSelf.currentImageName = name;
        [strongSelf.tearBookButton setBackgroundImage:[UIImage imageNamed:strongSelf.currentImageName] forState:UIControlStateNormal];
    });
}

- (IBAction)playTearBookVideo:(id)sender {
    if (self.alertView) {
        [self.alertView removeFromSuperview];
        self.alertView = nil;
    }
    if (self.currentVideoPath == nil) {
        self.currentVideoPath = [self.videoArray firstObject];
    } else {
        if (self.currentVideoPath == self.videoArray.lastObject) {
            return;
        }
        NSUInteger index = [self.videoArray indexOfObject:self.currentVideoPath];
        self.currentVideoPath = [self.videoArray objectAtIndex:index + 1];
    }
    if (!self.movie) {
        self.movie = [[MPMoviePlayerController alloc] initWithContentURL:self.currentVideoPath];
    } else {
        [self.movie setContentURL:self.currentVideoPath];
    }
    self.movie.controlStyle = MPMovieControlStyleNone;
    self.movie.initialPlaybackTime = -1;
    [self.movie.view setFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.width * 0.75)];
    self.movie.view.center = self.view.center;
    [self.view addSubview:self.movie.view];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(removeMovie:) name:MPMoviePlayerPlaybackDidFinishNotification object:self.movie];
    [self.movie play];
}

- (void)removeMovie:(NSNotification *)notify {
    MPMoviePlayerController *movie = [notify object];
    [movie.view removeFromSuperview];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:movie];
    if (self.currentVideoPath != [self.videoArray lastObject]) {
        [self showAlert];
    } else {
        //TODO:设置撕书后图片
#warning TODO
//        [self setBookButtonImageWithImageName:@""];
        //TODO:撕书后是否需要重置视频？
    }
    
}

- (IBAction)switchToLastBook:(id)sender {
    NSString *firstImageName = [self.bookArray firstObject];
    if ([self.currentImageName isEqualToString:firstImageName]) {
        return;
    }
    NSUInteger currentIndex = [self.bookArray indexOfObject:self.currentImageName];
    NSString *currentImageName = [self.bookArray objectAtIndex:currentIndex-1];
    [self setBookButtonImageWithImageName:currentImageName];
}

- (IBAction)switchToNextBook:(id)sender {
    NSString *lastImageName = [self.bookArray lastObject];
    if ([self.currentImageName isEqualToString:lastImageName]) {
        return;
    }
    NSUInteger currentIndex = [self.bookArray indexOfObject:self.currentImageName];
    NSString *currentImageName = [self.bookArray objectAtIndex:currentIndex+1];
    [self setBookButtonImageWithImageName:currentImageName];
}

- (IBAction)dismissViewController:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)showAlert {
    
    if (!self.alertView) {
        self.alertView = [[UIView alloc] initWithFrame:self.view.bounds];
        [self.alertView setBackgroundColor:[UIColor colorWithWhite:0.0 alpha:0.5]];
        self.alertView.userInteractionEnabled = YES;
    }
    [self.view addSubview:self.alertView];
    
    UIView *alertView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 100)];
    [alertView setBackgroundColor:[UIColor whiteColor]];
    alertView.center = self.alertView.center;
    [self.alertView addSubview:alertView];
    
    UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 150, 30)];
    textLabel.center = CGPointMake(alertView.bounds.size.width/2, alertView.bounds.size.height/2 - 20);
    [textLabel setText:@"继续撕书？"];
    textLabel.textAlignment = NSTextAlignmentCenter;
    [alertView addSubview:textLabel];
    
    UIButton *returnButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [returnButton setTitle:@"确定" forState:UIControlStateNormal];
    [returnButton setBackgroundColor:[UIColor cyanColor]];
    [returnButton addTarget:self action:@selector(playTearBookVideo:) forControlEvents:UIControlEventTouchUpInside];
    [returnButton setFrame:CGRectMake(textLabel.frame.origin.x, textLabel.frame.origin.y + textLabel.frame.size.height + 10, 60, 30)];
    [alertView addSubview:returnButton];
    
    UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    [cancelButton setBackgroundColor:[UIColor cyanColor]];
    [cancelButton addTarget:self action:@selector(dismissAlert) forControlEvents:UIControlEventTouchUpInside];
    [cancelButton setFrame:CGRectMake(textLabel.frame.origin.x + textLabel.frame.size.width - 60, returnButton.frame.origin.y, 60, 30)];
    [alertView addSubview:cancelButton];
}

- (void)dismissAlert {
    self.currentVideoPath = nil;
    [self.alertView removeFromSuperview];
    self.alertView = nil;
}

@end
